import sys
import wx
import os

from LMG.Utility.constants import *
from LMG.Utility.helpers import intersection

################################################################

class ActionHandler:
    """
    Separates out some of the methods that are solely used to
    deal with actions that occur in ABCList
    """
    def __init__(self):
        pass

    def procREMOVE(self, workinglist = None, removefiles = False):
        # Don't do anything if we don't have a list
        if not workinglist:
            return
            
        removetorrent = utility.config.Read('removetorrent', "boolean")
        
        # Temporarily stop updates for torrents that will be
        # affected by the move
        # (i.e.: ones that might generate the wxPython error:
        #        "unable to retrieve information about list control item x")
        #
        # The torrents affected are the ones beyond what the
        # new length of the list will be
        #
        # The torrents will get set to update again once their list indices
        # have been updated
        stopupdate = utility.torrents["all"][-len(workinglist):]
        for torrent in stopupdate:
            torrent.status.dontupdate = True
        
        indexremoved = []
        for torrent in workinglist:
            indexremoved.append(torrent.listindex)
            
            # Get the name of the .torrent and
            # the .info files
            src = torrent.src
            configfile = torrent.torrentconfig.filename
            
            torrent.shutdown()
            
            if removefiles:
                torrent.files.removeFiles()
            
            if removetorrent:
                # Remove .torrent file
                try:
                    os.remove(src)
                except:
                    pass
                # Remove .info file
                try:
                    os.remove(configfile)
                except:
                    pass
            
            torrent = None

        # Only need to update if we actually removed something
        if indexremoved:
            indexremoved.sort(reverse = True)
            
            for index in indexremoved:
                # Remove from the display
                utility.window.list.Select(index, False)
                utility.window.list.DeleteItem(index)
            
                # Remove from scheduler
                utility.torrents["all"].pop(index)

            utility.queue.updateListIndex(startindex = indexremoved[-1])
            utility.queue.updateAndInvoke()
            
    def procMOVE(self, workinglist = None):
        if workinglist is None:
            workinglist = utility.torrents["all"]

        update = [1 for torrent in workinglist if torrent.files.move()]
            
        if update:
            utility.queue.updateAndInvoke()

    def procSTOP(self, workinglist = None):
        if workinglist is None:
            workinglist = utility.torrents["all"]
            
        update = [1 for torrent in workinglist if torrent.actions.stop()]
            
        if update:
            utility.queue.updateAndInvoke()

    def procUNSTOP(self, workinglist = None):
        fulllist = utility.torrents["inactive"].keys()
        if workinglist is None:
            workinglist = fulllist
        else:
            workinglist = intersection(fulllist, workinglist)

        update = [1 for torrent in workinglist
                    if torrent.status.value == STATUS_STOP
                       and torrent.actions.queue()]

        if update:
            utility.queue.updateAndInvoke()
        
    def procPAUSE(self, workinglist = None, release = False):
        fulllist = utility.torrents["active"].keys()
        if workinglist is None:
            workinglist = fulllist
        else:
            workinglist = intersection(fulllist, workinglist)

        update = [1 for torrent in workinglist if torrent.actions.pause(release)]

        if update:
            utility.queue.UpdateRunningTorrentCounters()
       
    def procRESUME(self, workinglist = None, skipcheck = False):
        fulllist = utility.torrents["inactive"].keys()
        if workinglist is None:
            workinglist = fulllist
        else:
            workinglist = intersection(fulllist, workinglist)
        
        update = [1 for torrent in workinglist if torrent.actions.resume(skipcheck)]

        if update:
            utility.queue.updateAndInvoke(invokeLater = False)

    def procQUEUE(self, workinglist = None):
        fulllist = utility.torrents["inactive"].keys()
        if workinglist is None:
            workinglist = fulllist
        else:
            workinglist = intersection(fulllist, workinglist)

#        if workinglist is None:
#            workinglist = utility.torrents["all"]
        update = [1 for torrent in workinglist if torrent.actions.queue()]
        
        if update:
            utility.queue.updateAndInvoke()

    def procHASHCHECK(self, workinglist = None):
        if workinglist is None:
            workinglist = utility.torrents["all"]
        
        update = [1 for torrent in workinglist if torrent.actions.hashCheck()]
        
        if update:
            utility.queue.updateAndInvoke()
